#include "opening_rect.h"

OpeningRect::OpeningRect( Animation* src, float _threshold, int start, int end )
{
    w = h = 0;
    x = y = 0.5;
    threshold = _threshold;

    if( end == -1 ) end = src->GetDuration();

    wpg = new SimplePropertyGetter< float>( &w );
    wps = new WidthPropertySetter( this );
    pW = new Property< float >( "w", wpg, wps );

    hpg = new SimplePropertyGetter< float >( &h );
    hps = new HeightPropertySetter( this );
    pH = new Property< float >( "h", hpg, hps );

    wI = new ScalarInterpolator< int, float >( new Linear_NI< float >(),
                                               start, start + ( end - start ) * threshold,
                                               0.0f, 1.0f, true );
    wI->AttachTo( pW );
    src->GetTime()->AddObserver( wI );

    hI = new ScalarInterpolator< int, float >( new Linear_NI< float >(),
                                               start + ( end - start ) * threshold, end,
                                               0.0f, 1.0f, true );
    hI->AttachTo( pH );
    src->GetTime()->AddObserver( hI );
}

OpeningRect::~OpeningRect( void )
{
    delete pW;
    delete wpg;
    delete wps;

    delete pH;
    delete hpg;
    delete hps;

    delete wI;
    delete hI;
}

OpeningRect::WidthPropertySetter::WidthPropertySetter( OpeningRect* _owner )
{
    owner = _owner;
}

void OpeningRect::WidthPropertySetter::operator ()( const float& val )
{
    owner->w = val;
    owner->x = 0.5 - val/2;
}

OpeningRect::HeightPropertySetter::HeightPropertySetter( OpeningRect* _owner )
{
    owner = _owner;
}

void OpeningRect::HeightPropertySetter::operator()( const float& val )
{
    owner->h = val;
    owner->y = 0.5 - val/2;
}
